<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 社区个人中心管理器
 */
class Carefree extends MY_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->model('task_model');
        $this->load->model('subtasks_model');
        //广告轮标图
        $banner_list = $this->db->query("select banner_img,banner_link,title from tan_banner where type = 9 and status = 1 order by sort desc limit 5")->result_array();
        $this->assign("banner_list", $banner_list);
    }

    function index(){
        //1.获取3条曝光台的记录
        $pageIndex = isset($_REQUEST['pageIndex']) ? trim($_REQUEST['pageIndex']) : 0;
        $records_per_page = 3;
        $where = ' w.type=1 and w.is_show=1';
        $orderby = ' order by w.create_time desc';
        $sqlCount = "select w.* from tan_bbs_exposure w where $where";
        $sqlList = "select w.id,w.questioner_id,w.title,w.thumb,w.summary,w.tags_name,w.create_time,w.readnum,w.answernum,w.likes,w.focusnum,w.answer_id,w.answer_uid,u1.nickname wd_nickname,u1.head_img wd_headimg from tan_bbs_exposure w left join tan_users_info u1 on u1.user_id = w.questioner_id where $where $orderby limit ".($pageIndex * $records_per_page).",$records_per_page";//构造查询结果记录;
        $totalCount = $this->db->query($sqlCount)->num_rows();
        $exposure_list = $this->db->query($sqlList)->result_array();
        foreach ($exposure_list as $key=>$one){
            $exposure_list[$key]['id'] = encrypt_wenda_id($one['id']);
        }
        $this->assign("exposure_list", $exposure_list);
        //2.三个热点资讯的信息(35)
        $where = ' w.type=2 and w.is_show=1';
        $orderby = ' order by w.create_time desc';
        $sqlCount = "select w.* from tan_bbs_exposure w where $where";
        $sqlList = "select w.id,w.questioner_id,w.title,w.author,w.summary,w.thumb,w.tags_name,w.create_time,w.readnum,w.answernum,w.likes,w.focusnum,w.answer_id,w.answer_uid,u1.nickname wd_nickname,u1.head_img wd_headimg from tan_bbs_exposure w left join tan_users_info u1 on u1.user_id = w.questioner_id where $where $orderby limit ".($pageIndex * $records_per_page).",$records_per_page";//构造查询结果记录;
        $totalCount = $this->db->query($sqlCount)->num_rows();
        $information_list = $this->db->query($sqlList)->result_array();
        foreach ($information_list as $key=>$one){
            $information_list[$key]['id'] = encrypt_wenda_id($one['id']);
        }

        $this->assign("information_list", $information_list);
        //3.八个推荐任务
        $where = " t.cate_id=4 and t.is_end=0";
        $pageIndex = 0;
        $records_per_page = 8;
        $task_list= $this->task_model->task_list($where,$pageIndex+1,$records_per_page);
        foreach ($task_list as $key=>$val) {
            $sql = "select * from tan_subtasks where tid={$val['tid']} and is_end!=1";
            $subtask_list = $this->db->query($sql)->row_array();
            $can_recieve = empty($subtask_list) ? 1 : 0;
            if ($val['task_tag']) {
                $task_tags = explode(',', $val['task_tag']);
                $tag_span = "";
                foreach ($task_tags as $tag_key => $tag) {
                    $tag_span .= "<span class='label'>" . $tag . "</span>";
                }
                $task_list[$key]['tag_span'] = $tag_span;
            }
            $temp_tid = encrypt_wenda_id($val['tid']);
            $task_list[$key]['logo'] = $val['logo'] ? $val['logo'] : "/static/weixin/img/nopic.png";
            $task_list[$key]['can_recieve'] = $can_recieve;
            $link = "/task/{$temp_tid}.html";
            $task_list[$key]['commission'] = 1.0 * $val['commission'] / 10000;
            $task_list[$key]['link'] = $link;
        }
        $this->assign("task_list", $task_list);
        $this->display("carefree/index.html");
    }

    function information(){
        $pageIndex = isset($_REQUEST['pageIndex']) ? trim($_REQUEST['pageIndex']) : 0;
        $records_per_page = 10;
        $where = ' w.type=2 and w.is_show=1';
        $orderby = ' order by w.create_time desc';
        $sqlCount = "select w.* from tan_bbs_exposure w where $where";
        $sqlList = "select w.id,w.questioner_id,w.title,w.author,w.summary,w.thumb,w.tags_name,w.create_time,w.readnum,w.answernum,w.likes,w.focusnum,w.answer_id,w.answer_uid,u1.nickname wd_nickname,u1.head_img wd_headimg from tan_bbs_exposure w left join tan_users_info u1 on u1.user_id = w.questioner_id where $where $orderby limit ".($pageIndex * $records_per_page).",$records_per_page";//构造查询结果记录;
        $totalCount = $this->db->query($sqlCount)->num_rows();
        $page = new Paging($totalCount, $records_per_page);
        $information_list = $this->db->query($sqlList)->result_array();
        foreach ($information_list as $key=>$one){
            $information_list[$key]['id'] = encrypt_wenda_id($one['id']);
        }
        $this->assign('showPage', $page->showpage());
        $this->assign("information_list", $information_list);
        $this->display("carefree/information.html");
    }
    function exposure(){
        $pageIndex = isset($_REQUEST['pageIndex']) ? trim($_REQUEST['pageIndex']) : 0;
        $records_per_page = 15;
        $where = ' w.type=1 and w.is_show=1';
        $orderby = ' order by w.create_time desc';
        $sqlCount = "select w.* from tan_bbs_exposure w where $where";
        $sqlList = "select w.id,w.questioner_id,w.title,w.summary,w.tags_name,w.create_time,w.readnum,w.answernum,w.likes,w.focusnum,w.answer_id,w.answer_uid,u1.nickname wd_nickname,u1.head_img wd_headimg from tan_bbs_exposure w left join tan_users_info u1 on u1.user_id = w.questioner_id where $where $orderby limit ".($pageIndex * $records_per_page).",$records_per_page";//构造查询结果记录;
        $totalCount = $this->db->query($sqlCount)->num_rows();
        $page = new Paging($totalCount, $records_per_page);
        $exposure_list = $this->db->query($sqlList)->result_array();
        foreach ($exposure_list as $key=>$one){
            $exposure_list[$key]['id'] = encrypt_wenda_id($one['id']);
        }
        $this->assign('showPage', $page->showpage());
        $this->assign("exposure_list", $exposure_list);
        $this->display("carefree/exposure.html");
    }


    function detail($wenda_id,$answer_id,$reply_id){
        
        $this->display("carefree/detail.html");
    }

}